package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import org.impl.UserDAOProxy;
import org.vo.UserVo;
public class UserTable {
   private JFrame frame=new JFrame("用户信息");
   private   Object[] column={"ID","姓名","性别","密码"};
   private  Object[][] row=new Object[30][4];
   private DefaultTableModel model=null;
   private JTable table=null;            
   private JScrollPane scrollpane =null;
   private JButton update=new JButton("修改");
   private JButton delete=new JButton("删除");
	
	
	public UserTable() {
		// TODO Auto-generated constructor stub
		List<UserVo> user=null;                
		try {
			user=new UserDAOProxy().searchAll();    //获取数据库表格内容
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}    
		for(int i=0;i<user.size();i++){              //设置显示内容
			row[i][0]=user.get(i).getId();
			row[i][1]=user.get(i).getUserName();
			row[i][2]=user.get(i).getSex();
			row[i][3]=user.get(i).getPassword();
			
		}
		model=new DefaultTableModel(row, column){
			public boolean isCellEditable(int row,int column){
				return false;                                  //设置table不能被修改
			}
			    
		};       //显示JTable内容
		table=new JTable(model);
		table.getTableHeader().setReorderingAllowed(false); //让表格不能被移动列
		scrollpane=new JScrollPane(table);
		frame.setLayout(null);
		scrollpane.setBounds(10,10,516,320);
		update.setBounds(200,340,60, 30);
		delete.setBounds(290,340,60,30);
		frame.add(scrollpane);
		frame.add(update);
		frame.add(delete);
		frame.setSize(550,420);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		
		delete.addActionListener(new ActionListener(){         //删除操作

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int  i=table.getSelectedRow(); //删除选定行
				if(i==-1)
					{JOptionPane.showMessageDialog(null,"请选择要删除的行!");
					return ;
					}
				String sId=(String) row[i][0];
				UserVo user=new UserVo();
				user.setId(sId);
				try {
					int answer=JOptionPane.showConfirmDialog(null,"是否继续删除该信息？");
					if(answer==JOptionPane.YES_OPTION)
					 { new UserDAOProxy().delete(user);
					   model.removeRow(i);   //删除用户
					   model.addRow(new Object[]{});   //增加回一个空白行
					 }         
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}   
				
			}
			
		});
		
		update.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {    
				// TODO Auto-generated method stub
				int i=table.getSelectedRow(); 
				if(i==-1)
				{JOptionPane.showMessageDialog(null,"请选择要修改的行!");
				return ;
				}
				new UpdateUser(table,(String)table.getValueAt(i, 0),(String)table.getValueAt(i, 1),
						       (String)table.getValueAt(i, 2),(String)table.getValueAt(i, 3));               //调用修改界面
			}
			
		});
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     new UserTable();
	}

}
	
	